﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Web.Configuration;

namespace ForestryEnterprise
{
    public partial class AddTree : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.cancel.CausesValidation = false;
        }

        protected void add_Click(object sender, EventArgs e)
        {
            string statement = "select tree_name from Tree where tree_name = @treeN";
            OleDbConnection connection = new OleDbConnection(WebConfigurationManager.ConnectionStrings["forestryDB"].ConnectionString);
            OleDbCommand command = new OleDbCommand(statement, connection);
            command.Parameters.AddWithValue("@treeN", this.treeName.Text.Trim());
            connection.Open();
            
            if (command.ExecuteReader().HasRows)
            {
                this.err.Text = "Tree exists";
                connection.Close();
                return;
            }
            connection.Close();

            /*** Basic info ***/

            statement = "insert into Tree(tree_height, tree_name, tree_age, tree_width) values(@treeH, @treeN, @treeA, @treeW)";
            command = new OleDbCommand(statement, connection);
            command.Parameters.AddWithValue("@treeH", float.Parse(string.IsNullOrEmpty(treeHeight.Text) == true ? "0" : treeHeight.Text));
            command.Parameters.AddWithValue("@treeN", treeName.Text.Trim());
            command.Parameters.AddWithValue("@treeA", int.Parse(string.IsNullOrEmpty(treeAge.Text) == true ? "0" : treeAge.Text));
            command.Parameters.AddWithValue("@treeW", float.Parse(string.IsNullOrEmpty(treeWidth.Text) == true ? "0" : treeWidth.Text));
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();

            /** Get tree ID ***/

            statement = "select tree_id from Tree where tree_name = @treeN";
            command = new OleDbCommand(statement, connection);
            command.Parameters.AddWithValue("@treeN", this.treeName.Text.Trim());
            connection.Open();
            OleDbDataReader reader = command.ExecuteReader();
            reader.Read();
            int treeID = int.Parse(reader["tree_id"].ToString());
            reader.Close();
            connection.Close();

            /*** Seed info ***/

            if (!string.IsNullOrEmpty(this.seedColor.Text) || !string.IsNullOrEmpty(this.seedDesc.Text) || !string.IsNullOrEmpty(this.seedWeight.Text))
            {
                statement = "insert into Seed values(@treeID, @sDesc, @sColor, @sWeight)";
                command = new OleDbCommand(statement, connection);
                command.Parameters.AddWithValue("@treeID", treeID);
                command.Parameters.AddWithValue("@sDesc", this.seedDesc.Text);
                command.Parameters.AddWithValue("@sColor", this.seedColor.Text);
                command.Parameters.AddWithValue("@sWeight", float.Parse(string.IsNullOrEmpty(this.seedWeight.Text) == true ? "0" : this.seedWeight.Text));

                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }

            /*** Stem info ***/

            if (!string.IsNullOrEmpty(this.stemColor.Text) || !string.IsNullOrEmpty(this.stemDesc.Text) || !string.IsNullOrEmpty(this.stemHeight.Text) || !string.IsNullOrEmpty(this.stemWidth.Text))
            {
                statement = "insert into Stem values(@treeID, @stDesc, @stColor, @stWeight, @stHeight)";
                command = new OleDbCommand(statement, connection);
                command.Parameters.AddWithValue("@treeID", treeID);
                command.Parameters.AddWithValue("@stDesc", this.stemDesc.Text);
                command.Parameters.AddWithValue("@stColor", this.stemColor.Text);
                command.Parameters.AddWithValue("@stWeight", float.Parse(string.IsNullOrEmpty(this.stemWidth.Text) == true ? "0" : this.stemWidth.Text));
                command.Parameters.AddWithValue("@stHeight", float.Parse(string.IsNullOrEmpty(this.stemHeight.Text) == true ? "0" : this.stemHeight.Text));

                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }

            /*** Skin info ***/

            if (!string.IsNullOrEmpty(this.skinColor.Text) || !string.IsNullOrEmpty(this.skinDesc.Text) || !string.IsNullOrEmpty(this.skinThickness.Text))
            {
                statement = "insert into Skin values(@treeID, @skDesc, @skColor, @skThickness)";
                command = new OleDbCommand(statement, connection);
                command.Parameters.AddWithValue("@treeID", treeID);
                command.Parameters.AddWithValue("@skDesc", this.skinDesc.Text);
                command.Parameters.AddWithValue("@skColor", this.skinColor.Text);
                command.Parameters.AddWithValue("@skThickness", float.Parse(string.IsNullOrEmpty(this.skinThickness.Text) == true ? "0" : this.skinThickness.Text));

                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }

            /*** Leaf info ***/
            
            if (!string.IsNullOrEmpty(this.leafColor.Text) || !string.IsNullOrEmpty(this.leafDesc.Text) || !string.IsNullOrEmpty(this.leafThickness.Text) || !string.IsNullOrEmpty(this.leafProportion.Text))
            {
                statement = "insert into Leaf values(@treeID, @lDesc, @lColor, @lThickness, @lProportions)";
                command = new OleDbCommand(statement, connection);
                command.Parameters.AddWithValue("@treeID", treeID);
                command.Parameters.AddWithValue("@lDesc", this.leafDesc.Text);
                command.Parameters.AddWithValue("@lColor", this.leafColor.Text);
                command.Parameters.AddWithValue("@lThickness", float.Parse(string.IsNullOrEmpty(this.leafThickness.Text) == true ? "0" : this.leafThickness.Text));
                command.Parameters.AddWithValue("@lProportions", this.leafProportion.Text);

                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }

            /*** Fruit info ***/

            if (!string.IsNullOrEmpty(this.fruitColor.Text) || !string.IsNullOrEmpty(this.fruitDesc.Text))
            {
                statement = "insert into Fruit values(@treeID, @fDesc, @fColor, @fEatable)";
                command = new OleDbCommand(statement, connection);
                command.Parameters.AddWithValue("@treeID", treeID);
                command.Parameters.AddWithValue("@fDesc", this.fruitDesc.Text);
                command.Parameters.AddWithValue("@fColor", this.fruitColor.Text);
                command.Parameters.AddWithValue("@fEatable", this.fruitEatable.Checked == true ? 1 : 0);

                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }

            Response.Redirect("Tree.aspx");
        }

        protected void cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Tree.aspx");
        }
    }
}